// packageC/orders/LotteryRecord/LotteryRecord.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    recordsList: [],
    activity_name: "",
    lotteryId: "",
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    imgPath: app.globalData.imgPath,
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.lotteryId) {
      this.setData({
        lotteryId: options.lotteryId
      });
    }
    this.initData();
    this.getData();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  initData() {
    this.setData({
      activity_name: "",
      recordsList: [],
      page: 1,
      total_page: 0,
      isLoadMore: true
    });
  },
  getData() {
    let that = this;
    let json = {
      lotteryId: this.data.lotteryId
    };
    let urlStr = app.getNetAddresss("plugin.lucky-draw.frontend.draw.getMyRecord");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            amount_word: res.data.amount_word,
            love_word: res.data.love_word,
            point_word: res.data.point_word,
            isLoadMore: true,
            page: 1,
            activity_name: res.data.activity_name,
            total_page: res.data.my_record.last_page
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
          that.setData({
            recordsList: res.data.my_record.data
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    let that = this;
    let json = {
      page: that.data.page,
      lotteryId: this.data.lotteryId
    };
    let urlStr = app.getNetAddresss("plugin.lucky-draw.frontend.draw.getMyRecord");
    that.setData({
      isLoadMore: false
    });
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      app._getNetWork({
        url: urlStr,
        data: json,
        success: function(resdata) {
          var res = resdata.data;
          that.setData({
            isLoadMore: true
          });
          if (res.result == 1) {
            var myData = res.data.my_record.data;
            that.setData({
              recordsList: that.data.recordsList.concat(myData)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  }
});
